﻿
// GameDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Game.h"
#include "GameDlg.h"
#include "afxdialogex.h"
#include "thread_control.h"
#include "log.h"
#include "resource.h"
#include "CppSQLite3.h"
#include "scriptMaple.h"
#include "ScriptWls.h"
#include "script.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif



TCHAR* arr[][2] = {
	{ L"hd923", L"华东923" },
	{ L"hd924", L"华东924" },
	{ L"hd925", L"华东925" },
	{ L"hb943", L"华北943" },
	{ L"hb944", L"华北944" },
	{ L"hb945", L"华北945" },
	{ L"hdbmdx", L"华东北美电信" },
	{ L"hdbmyd", L"华东北美移动" },
	{ L"hdbmduo", L"华东北美多线" },
	{ L"ef376", L"华东俄服376" },
	{ L"rb905", L"日本905" },
};

HWND   g_main_hwnd = NULL;
CGameDlg * g_main_cwnd = NULL;

void UpdateUI(long index, long action);

// CGameDlg 对话框



CGameDlg::CGameDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_GAME_DIALOG, pParent)
	, m_debug(FALSE)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_INFO, m_list_info);
	DDX_Control(pDX, IDC_LIST1, m_lstLine);
	DDX_Control(pDX, IDC_LIST2, m_listChangeLine);
	DDX_Control(pDX, IDC_CHECK2, m_trade);
	DDX_Control(pDX, IDC_CHECK1, m_ZhaKun);
	DDX_Check(pDX, IDC_CHECK3, m_debug);
	DDX_Control(pDX, IDC_CHECK4, m_wlsYou);
	DDX_Control(pDX, IDC_CHECK5, m_zhaKunYou);
}

BEGIN_MESSAGE_MAP(CGameDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_Memu_ImportAccount, &CGameDlg::OnMemuImportaccount)
	ON_NOTIFY(NM_RCLICK, IDC_LIST_INFO, &CGameDlg::OnNMRClickListInfo)
	ON_COMMAND(ID_Memu_ClearAccount, &CGameDlg::OnMemuClearaccount)
	ON_BN_CLICKED(IDC_BTN_HANDLE_BIND, &CGameDlg::OnBnClickedBtnHandleBind)
	ON_BN_CLICKED(IDC_BUTTON1, &CGameDlg::OnBnClickedButton1)
	ON_COMMAND(ID_Memu_Suspend_Choosed, &CGameDlg::OnMemuSuspendChoosed)
	ON_COMMAND(ID_Menu_Continue_Choosed, &CGameDlg::OnMenuContinueChoosed)
	ON_COMMAND(ID_32771, &CGameDlg::On32771)
	ON_COMMAND(ID_32772, &CGameDlg::On32772)
	ON_COMMAND(ID_SetFinish, &CGameDlg::OnSetfinish)
	ON_BN_CLICKED(IDC_BTN_CLOSE_ALL, &CGameDlg::OnBnClickedBtnCloseAll)
	ON_BN_CLICKED(IDC_BUTTON2, &CGameDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BTN_SAVE, &CGameDlg::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BUTTON_MOVE, &CGameDlg::OnBnClickedButtonMove)
	ON_BN_CLICKED(IDC_BUTTON_UP, &CGameDlg::OnBnClickedButtonUp)
	ON_BN_CLICKED(IDC_BUTTON_DOWN, &CGameDlg::OnBnClickedButtonDown)
	ON_BN_CLICKED(IDC_BUTTON_DEL, &CGameDlg::OnBnClickedButtonDel)
	ON_COMMAND(ID_32773, &CGameDlg::OnNextRole)
	ON_BN_CLICKED(IDC_BTN_BACK, &CGameDlg::OnBnClickedBtnBack)
	ON_BN_CLICKED(IDC_BTN_REST, &CGameDlg::OnBnClickedBtnRest)
END_MESSAGE_MAP()


// CGameDlg 消息处理程序

BOOL CGameDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

									// TODO: 在此添加额外的初始化代码
	g_main_hwnd = GetSafeHwnd();
	g_dm->MoveWindow((long)this->m_hWnd, 500, 300);
	//g_dm->IsFileExist()
	
	//if (g_dm->IsFolderExist(L"d:\\pic")==0) {
	//	g_dm->CreateFolder(L"d:\\pic");
	//}
	int k = 0;
	for (size_t i = 0; i < 12; i++)
	{
		
		int intX = 128;
		intX = 128 + k * 122 - 5;
		g_Point[i][0] = intX;

		if (i < 6) {
			g_Point[i][1] = 300;
			k++;
		}
		else {
			g_Point[i][1] = 510;
			k++;
		}
		if (k == 6) {
			k = 0;
		}

	}

	CString str = L"D:\\pic";
	if (!PathIsDirectory(str))
	{
		::CreateDirectory(str, NULL);//创建目录,已有的话不影响
	}
	g_main_cwnd = this;
	CListBox* list = (CListBox*)GetDlgItem(IDC_LIST1);
	TRACE(arr[0][0]);
	size_t size = sizeof(arr) / sizeof(TCHAR*[2]);
	for (size_t i = 0; i < size; i++)
	{
		list->AddString(arr[i][1]);
		/*arrLineCode[i] = arr[i][0];
		TRACE(arrLineCode[i]);*/
	}
	CString strLine = AfxGetApp()->GetProfileStringW(L"cfg", L"LineList");
	CStringArray arr;
	StringSplit(strLine, arr, ',');

	for (size_t i = 0; i < arr.GetCount(); i++)
	{
		m_listChangeLine.InsertString(i, arr.GetAt(i));
	}
	SetDlgItemTextW(IDC_EDIT1, AfxGetApp()->GetProfileStringW(L"cfg", L"PATH"));
	
	ListView_SetExtendedListViewStyle(m_list_info.GetSafeHwnd(),
		LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP | LVS_EX_GRIDLINES);    //网格线 //LVS_EX_CHECKBOXES
	int nIndex = 0;
	m_list_info.InsertColumn(nIndex++, _T("序号"), LVCFMT_CENTER, 40);//0
	m_list_info.InsertColumn(nIndex++, _T("账号"), LVCFMT_LEFT, 80);//2
	m_list_info.InsertColumn(nIndex++, _T("密码"), LVCFMT_LEFT, 80);//3
	m_list_info.InsertColumn(nIndex++, _T("二密"), LVCFMT_CENTER, 80);//4
	m_list_info.InsertColumn(nIndex++, _T("角色区服"), LVCFMT_CENTER, 60);//5
	m_list_info.InsertColumn(nIndex++, _T("任务状态"), LVCFMT_CENTER, 100);//9
	m_list_info.InsertColumn(nIndex++, _T("运行状态"), LVCFMT_CENTER, 80);//10
	m_list_info.InsertColumn(nIndex++, _T("完成次数"), LVCFMT_CENTER, 60);//10
	m_list_info.InsertColumn(nIndex++, _T("交易情况"), LVCFMT_CENTER, 60);//10
	m_list_info.InsertColumn(nIndex++, _T("背包金币"), LVCFMT_CENTER, 0);//10
	m_list_info.InsertColumn(nIndex++, _T("扎昆角色"), LVCFMT_CENTER, 80);//10
	m_list_info.InsertColumn(nIndex++, _T("子账号"), LVCFMT_CENTER, 80);//10
	m_list_info.InsertColumn(nIndex++, _T("线路"), LVCFMT_CENTER, 50);//10



	g_IniFile.SetIniFileName(ConfigFile);
	g_AccountFile.SetIniFileName(AccountFile);
	InitConfig();
	CppSQLite3DB db;
	CFileFind fFind;

	if (fFind.FindFile(_T("C:\\MobileGame\\DMTool.db")))
	{
		db.open("C:\\MobileGame\\DMTool.db", "ppo00123");
		CppSQLite3Query query = db.execQuery("SELECT * FROM ocrDetail where status='0' order by id desc");
		//ocrCmp.RemoveAll();
		ocrMap.clear();
		while (!query.eof())
		{
			
			OcrInfo ocrInfo;
			ocrInfo.sName = query.fieldValue("name");
			ocrInfo.sOcrText = query.fieldValue("ocrText");
			ocrInfo.sAction = query.fieldValue("action");
			ocrInfo.nCount = atoi(query.fieldValue("count") == NULL ? 0 : query.fieldValue("count"));
			ocrInfo.nDelay = atoi(query.fieldValue("delay") == NULL ? 0 : query.fieldValue("delay"));
			ocrInfo.sEnlarge = query.fieldValue("enlarge");
			ocrInfo.sInStr = query.fieldValue("instr");
			ocrInfo.sOffset = query.fieldValue("offset");
			ocrInfo.sPoint = query.fieldValue("point");
			ocrInfo.sType = query.fieldValue("type");
			ocrInfo.sDescribe = query.fieldValue("describe");
			ocrMap[CString(query.fieldValue("name"))] = ocrInfo;
			query.nextRow();
		}
		db.close();


		db.open("C:\\MobileGame\\DMTool.db", "ppo00123");
		query = db.execQuery("SELECT * FROM ocrInfo where status='0' order by id desc");
		//ocrCmp.RemoveAll();
		//ocrMap.clear();
		while (!query.eof())
		{
			OcrBase ocrInfo;
			ocrInfo.strName = query.fieldValue("name");
			ocrInfo.strOcrText = query.fieldValue("ocrText");
			ocrInfo.strAction = query.fieldValue("action");
			ocrInfo.strType = query.fieldValue("type");
			ocrInfo.nCount = atoi(query.fieldValue("count") == NULL ? 0 : query.fieldValue("count"));
			ocrInfo.nDelay = atoi(query.fieldValue("delay") == NULL ? 0 : query.fieldValue("delay"));
			ocrInfo.strEnlarge = query.fieldValue("enlarge");
			ocrInfo.strPoint = query.fieldValue("point");
			ocrInfo.strOffsetCol = query.fieldValue("offsetCol");
			ocrInfo.strOffsetPoint = query.fieldValue("offsetPoint");
			ocrInfo.fSim = atof(query.fieldValue("sim"));
			ocrInfo.strRndPoint = query.fieldValue("rndPoint");
			baseMap[ocrInfo.strName] = ocrInfo;
			query.nextRow();

		}
		db.close();
	}


	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CGameDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}


//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。

HCURSOR CGameDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



#pragma region 右键菜单

//显示右键菜单
void CGameDlg::OnNMRClickListInfo(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);


	// TODO: 在此添加控件通知处理程序代码
	CPoint ptCurSel(0, 0);
	GetCursorPos(&ptCurSel);
	//转换为当前控件坐标系的坐标
	m_list_info.ScreenToClient(&ptCurSel);
	LVHITTESTINFO HitTestInfo;
	HitTestInfo.pt = ptCurSel;
	HitTestInfo.flags = LVHT_ABOVE;
	//判断坐标命中第几项
	int nItem = m_list_info.SubItemHitTest(&HitTestInfo);
	//如果有选中项
	//弹出菜单
	CMenu menu;
	menu.LoadMenu(IDR_MENU1);
	CPoint point;
	GetCursorPos(&point);
	CMenu* pSubMenu = menu.GetSubMenu(0);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);

	*pResult = 0;
}

//导入账号
void CGameDlg::OnMemuImportaccount()
{
	// TODO: 在此添加命令处理程序代码
	CString strFileName, strPathName;
	m_list_info.DeleteAllItems();
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, _T("文本文件(*.txt)|*.txt||"));
	if (dlg.DoModal() == IDOK)
	{
		strFileName = dlg.GetPathName();
		CStdioFileEx sFile;

		CStringArray ArrAcountInfo;
		if (!strFileName.IsEmpty())
		{
			sFile.Open(strFileName, CFile::modeRead);
			CString strLine;
			while (sFile.ReadString(strLine))
			{
				CString strTemp = strLine;
				int len = strLine.GetLength();
				if (len > 100)
				{
					sFile.Close();
					return;
				}
				//zhanghao-mima-daqu
				strLine.Replace(_T("----"), _T("-"));
				strLine.Replace(_T("---"), _T("-"));
				strLine.Replace(_T("--"), _T("-"));

				StringSplit(strLine, ArrAcountInfo, '-');

				int	iBegin = m_list_info.GetItemCount();

				int iRow = m_list_info.InsertItem(iBegin, NumberToString(iBegin));
				m_list_info.SetItemText(iRow, 1, ArrAcountInfo[0]);//瑯뵀
				m_list_info.SetItemText(iRow, 2, ArrAcountInfo[1]);//쵱쯤
				m_list_info.SetItemText(iRow, 3, ArrAcountInfo[2]);//댕혐
				m_list_info.SetItemText(iRow, 4, ArrAcountInfo[3]);//댕혐
				CString strLineCode=L"0123456789";
				int nCount = ArrAcountInfo.GetCount();
				int nLen = 0;
				if (nCount<=6) {
					//if (strLineCode.Find(ArrAcountInfo[4]) > 0) {//存在子账号
					//	m_list_info.SetItemText(iRow, 12, ArrAcountInfo[4]);
					//}
					//else {
					//	m_list_info.SetItemText(iRow, 11, ArrAcountInfo[4]);
					//}
					if (nCount == 5) {
						m_list_info.SetItemText(iRow, 12, ArrAcountInfo[4]);
					}
					if (nCount == 6) {
						nLen=ArrAcountInfo[5].GetLength();
						CString cl = ArrAcountInfo[5];
						int nnn = ArrAcountInfo[5].Find(L"i");
						//是否是扎昆角色
						if (ArrAcountInfo[5].Find(L"$") >=0) {
							m_list_info.SetItemText(iRow, 12, ArrAcountInfo[4]);
							m_list_info.SetItemText(iRow, 10, ArrAcountInfo[5].Mid(1, nLen - 1));
						}
						else {
							m_list_info.SetItemText(iRow, 11, ArrAcountInfo[4]);
							m_list_info.SetItemText(iRow, 12, ArrAcountInfo[5]);
						}
					}
					
				}
				else if (ArrAcountInfo.GetCount() == 7) {
					m_list_info.SetItemText(iRow, 11, ArrAcountInfo[4]);
					m_list_info.SetItemText(iRow, 12, ArrAcountInfo[5]);
					nLen = ArrAcountInfo[6].GetLength();
					m_list_info.SetItemText(iRow, 10, ArrAcountInfo[6].Mid(1, nLen-1));


				}
				
				m_list_info.SetItemText(iRow, 6, L"未完成");//댕혐
				if (!ArrAcountInfo[0].IsEmpty() && !ArrAcountInfo[1].IsEmpty())//&& !ArrAcountInfo[2].IsEmpty())
				{
				
					g_AccountFile.SetString(NumberToString(iRow + 1), L"code", ArrAcountInfo[0]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"password", ArrAcountInfo[1]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"twocode", ArrAcountInfo[2]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"server", ArrAcountInfo[3]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"RoleNum",L"0");
					if (ArrAcountInfo.GetCount() <= 6) {
						CString codeLine = ArrAcountInfo[4];
						if (ArrAcountInfo.GetCount() == 5) {
							g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[4]);
						}
						
						if (nCount == 6) {
							nLen = ArrAcountInfo[5].GetLength();
							if (ArrAcountInfo[5].Find(L"$") >= 0) {
								g_AccountFile.SetString(NumberToString(iRow + 1), L"ZhaNum", ArrAcountInfo[5].Mid(1, nLen - 1));
								g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[4]);

							}
							else {
								g_AccountFile.SetString(NumberToString(iRow + 1), L"childCode", ArrAcountInfo[4]);
								g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[5]);

							}
						}
						
					}if (ArrAcountInfo.GetCount() == 7) {
						nLen = ArrAcountInfo[6].GetLength();
						g_AccountFile.SetString(NumberToString(iRow + 1), L"ZhaNum", ArrAcountInfo[6].Mid(1, nLen-1));
						g_AccountFile.SetString(NumberToString(iRow + 1), L"childCode", ArrAcountInfo[4]);
						g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[5]);

					}
					 

					g_AccountFile.SetString(NumberToString(iRow + 1), L"state", L"0");

				}

			}
			sFile.Close();
		}
	}
}

//清空账号
void CGameDlg::OnMemuClearaccount()
{
	// TODO: 在此添加命令处理程序代码
	if (AfxMessageBox(L"你确定清空账号吗？", MB_YESNO) == IDYES)
	{
		m_list_info.DeleteAllItems();
		DeleteFile(L".\\Account.ini");
	}
}

//暂停线程
void CGameDlg::OnMemuSuspendChoosed()
{
	// TODO: 在此添加命令处理程序代码
	POSITION pos = m_list_info.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		Log(_T("暂停线程"));
		return;
	}

	long list_index = (long)m_list_info.GetNextSelectedItem(pos);
	long index = _tstoi(m_list_info.GetItemText(list_index, 0));
	ThreadPause(index);
}
//恢复线程
void CGameDlg::OnMenuContinueChoosed()
{

	// TODO: 在此添加命令处理程序代码
	POSITION pos = m_list_info.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		Log(_T("恢复线程"));
		return;
	}
	long list_index = (long)m_list_info.GetNextSelectedItem(pos);
	long index = _tstoi(m_list_info.GetItemText(list_index, 0));
	ThreadResume(index);
}



//置未完成
void CGameDlg::On32771()
{
	// TODO: 在此添加命令处理程序代码
	int nIndex =m_list_info.GetSelectionMark();
	if (nIndex == -1) {
		return;
	}
	// TODO: 在此添加命令处理程序代码
	if (AfxMessageBox(L"你确定更改状态吗？", MB_YESNO) == IDYES)
	{
		g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"0");
		m_list_info.SetItemText(nIndex, 6, L"未完成");
	}

}

//置boss完成
void CGameDlg::On32772()
{
	int nIndex = m_list_info.GetSelectionMark();
	if (nIndex == -1) {
		return;
	}
	// TODO: 在此添加命令处理程序代码
	if (AfxMessageBox(L"你确定更改状态吗？", MB_YESNO) == IDYES)
	{
		g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"6");
		m_list_info.SetItemText(nIndex, 6, L"Boss已完成");
	}
	// TODO: 在此添加命令处理程序代码
}

//置状态完成
void CGameDlg::OnSetfinish()
{
	// TODO: 在此添加命令处理程序代码
	int nIndex = m_list_info.GetSelectionMark();
	if (nIndex == -1) {
		return;
	}
	// TODO: 在此添加命令处理程序代码
	if (g_AccountFile.GetInt(NumberToString(nIndex + 1), L"state") == 1) {
		AfxMessageBox(L"该状态已完成不能设定？");
		return;
	}
	if (AfxMessageBox(L"你确定更改状态吗？", MB_YESNO) == IDYES)
	{
		//g_pPage1->OnBnClickedBtntaskstate();
		
		g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"1");
		m_list_info.SetItemText(nIndex, 6, L"完成");

		//如果选中扎昆
		if (m_ZhaKun.GetCheck() == 1) { 
			//int nRoleNum = g_AccountFile.GetInt(NumberToString(nIndex + 1), L"RoleNum");
			int nZhaNum = g_AccountFile.GetInt(NumberToString(nIndex + 1), L"ZhaNum");
			g_AccountFile.SetInt(NumberToString(nIndex + 1), L"RoleNum", nZhaNum);
			//g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"1");
			g_nState = 10;
		}
		
	}
}

#pragma endregion



#pragma region 自定义函数
//初始化配置
void CGameDlg::InitConfig()
{
	m_list_info.DeleteAllItems();
	CStringArray Sections;
	int iCount = g_AccountFile.GetSections(Sections);
	if (iCount <= 0)
	{
		return;
	}
	CString str; //获取系统时间 　　
	CTime tm; tm = CTime::GetCurrentTime();
	str = tm.Format("%H");
	CString sDate = g_IniFile.GetString(L"Set", L"AppDate");
	if (_wtoi(str) >= 17 && _wtoi(str) < 23) {
		this->m_ZhaKun.SetCheck(0);
	}
	else {
		this->m_ZhaKun.SetCheck(1);
	}
	for (int i = 0; i < iCount; i++)
	{
		int ItemCount = m_list_info.InsertItem(m_list_info.GetItemCount(), NumberToString(i));
		m_list_info.SetItemText(ItemCount, 1, g_AccountFile.GetString(NumberToString(i + 1), _T("code")));
		m_list_info.SetItemText(ItemCount, 2, g_AccountFile.GetString(NumberToString(i + 1), _T("password")));
		m_list_info.SetItemText(ItemCount, 3, g_AccountFile.GetString(NumberToString(i + 1), _T("twocode")));
		m_list_info.SetItemText(ItemCount, 4, g_AccountFile.GetString(NumberToString(i + 1), _T("server")));
		m_list_info.SetItemText(ItemCount, 6, arrState[g_AccountFile.GetInt(NumberToString(i + 1), _T("state"))]);
		m_list_info.SetItemText(ItemCount, 8, g_AccountFile.GetString(NumberToString(i + 1), _T("bbjb")));
		m_list_info.SetItemText(ItemCount, 11, g_AccountFile.GetString(NumberToString(i + 1), _T("childCode")));
		m_list_info.SetItemText(ItemCount, 12, g_AccountFile.GetString(NumberToString(i + 1), _T("CodeLine")));
		m_list_info.SetItemText(ItemCount, 10, g_AccountFile.GetString(NumberToString(i + 1), _T("ZhaNum")));
		m_list_info.SetItemText(ItemCount, 7, g_AccountFile.GetString(NumberToString(i + 1), _T("RoleNum")));
	}
}

//更新UI
void UpdateUI(long index, long action)
{
	//g_main_cwnd->UpdateList(index, action);
}

#pragma endregion


#pragma region 控件事件
//按钮开始
void CGameDlg::OnBnClickedBtnHandleBind()
{
	// TODO: 在此添加控件通知处理程序代码
	//线程开启
	CString str;
	size_t size = sizeof(arr) / sizeof(TCHAR*[2]);
	for (size_t i = 0; i < m_listChangeLine.GetCount(); i++)
	{
		m_listChangeLine.GetText(i, str);
		for (size_t j= 0; j < size; j++)
		{
			if (arr[j][1] == str) {
				arrLineCode[i+1] = arr[j][0];
				break;
			}

		}
		
	}
	GetDlgItem(IDC_BTN_HANDLE_BIND)->EnableWindow(FALSE);
	//MainTask(2);
	//CloseHandle(::CreateThread(NULL, 0, StartThread, NULL, 0, NULL));
	GetDlgItem(IDC_BTN_CLOSE_ALL)->EnableWindow(TRUE);
	ThreadStart();

	//if (!ThreadStart())
	//{
	//	g_dm->TerminateProcess(g_dm->GetWindowProcessId(hwnd));
	//}
}



//停止游戏
void CGameDlg::OnBnClickedBtnCloseAll()
{
	if (AfxMessageBox(L"你确定停止游戏吗？", MB_YESNO) == IDYES)
	{
		// TODO: 在此添加控件通知处理程序代码
		GetDlgItem(IDC_BTN_HANDLE_BIND)->EnableWindow(TRUE);
		ThreadStop(0);
	}
}


#pragma endregion

unsigned WINAPI SubThread1(PVOID pParam)
{
	//g_dm->SetSimMode(1);
	//long hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");

	CDm * dm;



	// 初始化当前线程com组件为MTA模式
	CoInitializeEx(NULL, 0);
	// 创建对象
	g_info[0].dm = new CDm;
	dm = g_info[0].dm;
	int ret = 0;
	//if (g_dm->GetBindWindow() == 0) {

	//ret= dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
	//dm->SetWindowState(hwnd, 12);
	//}
	// 检测对象是否创建成功,虽然这个一般不会失败,但为了程序健壮性考虑还是加上,如果内存吃紧，还是可能会失败
	if (dm == NULL || dm->Ver().GetLength() == 0)
	{
		Log(_T("对象创建失败"));
		ThreadNotifyUI_Post(NOTIFY_STOP, 0);
		return 0;
	}
	// 开启全局共享字库
	dm->EnableShareDict(1);

	// 其他设置,比如路径等等 当前目录
	ret = dm->SetPath(_T("c:\\MobileGame\\pic\\"));
	//g_main_cwnd->AppendText(L"绑定图片" + NumberToString(ret));
	dm->SetDict(0, L"jinbi.txt");
	dm->SetDict(1, L"code.txt");

	dm->SetShowErrorMsg(0);

	long hwnd = dm->GetBindWindow();
	if (hwnd > 0) {
		dm->UnBindWindow();
	}
	hwnd = dm->FindWindowEx(0, L"MapleStoryClass", L"MapleStory");
	ShowLog(L"等待启动.窗口句柄" + NumberToString(hwnd));
	ScriptDelay(0, 1000);
	//int ret = 0;
	if (hwnd != 0) {
		int ret = dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
		if (ret == 1) {
			dm->MoveWindow(hwnd, 0, 0);
			ShowLog(L"窗口绑定成功");
		}
	}
	dm->SetWindowState(hwnd, 12);

	Sleep(1000);
	//CString str = dm->FindPicExS(170, 213, 200, 228, L"b0.bmp|b1.bmp|b2.bmp|b3.bmp", L"050505", 0.95, 0);
	//str = dm->SortPosDistance(str, 3, 65535, 0);
	//AfxMessageBox(str);
	//ZhengLi(0, 0);
	Trade(0, 0);
	//ClearEquip(0);
	//TaskZhaKun(0, 0);
	//TaskWls(0, 0);
	//ExitZkjt(0);
	//if (LoginClientWls(0, 0)) {
	//	LoginGameWls(0, 0);
	//}
	//WlsYouPiao(0, 0);
	//ZkYouPiao(0, 0);
	return 0;

}

void CGameDlg::OnBnClickedButton1()
{
	long x, y,x1,y1;
	int a = m_trade.GetCheck();
	// TODO: 在此添加控件通知处理程序代码
	//long hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");

	//long hwnd = g_dm->FindWindowExW(0, L"", L"MapleStory");
	//CString strhwnd;
	//strhwnd.Format(L"%d", hwnd);
	//this->AppendText(strhwnd);
	//g_dm->GetWindowProcessId(L"MapleStory.exe");

	
	//return;
	//
	

	_beginthreadex(0, 0, SubThread1, NULL, 0, 0);
	return;
	
}


void CGameDlg::AppendText(CString str)
{
	int nCount = ((CEdit*)GetDlgItem(IDC_EDIT2))->GetLineCount();
	((CEdit*)GetDlgItem(IDC_EDIT2))->SetSel(GetDlgItem(IDC_EDIT2)->GetWindowTextLength(), GetDlgItem(IDC_EDIT2)->GetWindowTextLength());
	((CEdit*)GetDlgItem(IDC_EDIT2))->ReplaceSel(NumberToString(nCount)+L": "+ str + L"\r\n");

	if (((CEdit*)GetDlgItem(IDC_EDIT2))->GetLineCount()>200) {
		SetDlgItemTextW(IDC_EDIT2,L"");
	}
		
}






void CGameDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog dlg(TRUE, NULL, NULL, 4 | 2, L"数据文件(*exe)|*.exe");
	CString m_strGamePath;
	if (dlg.DoModal() == IDOK) {
		m_strGamePath = dlg.GetPathName();
		SetDlgItemTextW(IDC_EDIT1, m_strGamePath);
	}
}


void CGameDlg::OnBnClickedBtnSave()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	GetDlgItemTextW(IDC_EDIT1, str);
	AfxGetApp()->WriteProfileStringW(_T("cfg"), L"PATH", str);
	CString strList;
	for (size_t i = 0; i < m_listChangeLine.GetCount(); i++)
	{
		CString str;
		m_listChangeLine.GetText(i, str);
		if (i == m_listChangeLine.GetCount() - 1) {
			strList += str;
		}
		else {
			
			strList += str + L",";
		}
	}
	AfxGetApp()->WriteProfileStringW(_T("cfg"), L"LineList", strList);

	
}


void CGameDlg::OnBnClickedButtonMove()
{
	// TODO: 在此添加控件通知处理程序代码
	//获取左边列表选中
	int nSel=m_lstLine.GetCurSel();
	CString str,str1;
	m_lstLine.GetText(nSel, str);
	for (size_t i = 0; i < m_listChangeLine.GetCount(); i++)
	{

		m_listChangeLine.GetText(i, str1);
		if (str == str1) {
			AfxMessageBox(L"不能重复添加线路");
			return;
		}
	}
	//int nIndex = ==0?
	m_listChangeLine.InsertString(m_listChangeLine.GetCount(), str);

}


void CGameDlg::OnBnClickedButtonUp()
{
	// TODO: 在此添加控件通知处理程序代码
	int nSel = m_listChangeLine.GetCurSel();
	if (nSel == -1) {
		AfxMessageBox(L"请先选中数据");
		return;
	}
	if (nSel == 0) {
		AfxMessageBox(L"已经为最上层数据");
		return;
	}
	CString str,str1;
	m_listChangeLine.GetText(nSel, str);
	m_listChangeLine.GetText(nSel-1, str1);
	m_listChangeLine.DeleteString(nSel);
	m_listChangeLine.DeleteString(nSel-1);
	m_listChangeLine.InsertString(nSel - 1, str);
	m_listChangeLine.InsertString(nSel, str1);
}


void CGameDlg::OnBnClickedButtonDown()
{
	// TODO: 在此添加控件通知处理程序代码
	int nSel = m_listChangeLine.GetCurSel();
	if (nSel == -1) {
		AfxMessageBox(L"请先选中数据");
		return;
	}
	if (nSel+1 == m_listChangeLine.GetCount()) {
		AfxMessageBox(L"已经为最底层数据");
		return;
	}
	CString str, str1;
	m_listChangeLine.GetText(nSel, str);
	m_listChangeLine.GetText(nSel + 1, str1);
	
	m_listChangeLine.DeleteString(nSel + 1);
	m_listChangeLine.DeleteString(nSel);
	m_listChangeLine.InsertString(nSel, str1);
	m_listChangeLine.InsertString(nSel + 1, str);
	
}


void CGameDlg::OnBnClickedButtonDel()
{
	// TODO: 在此添加控件通知处理程序代码
	int nSel = m_listChangeLine.GetCurSel();
	if (nSel == -1) {
		AfxMessageBox(L"请先选中数据");
		return;
	}
	if (m_listChangeLine.GetCount() == 0) {
		AfxMessageBox(L"请先添加数据");
		return;
	}
	m_listChangeLine.DeleteString(nSel);
}


//切换到下一个角色
void CGameDlg::OnNextRole()
{
	// TODO: 在此添加命令处理程序代码
	int nIndex = m_list_info.GetSelectionMark();
	if (nIndex == -1) {
		return;
	}
	// TODO: 在此添加命令处理程序代码
	if (g_AccountFile.GetInt(NumberToString(nIndex + 1), L"state") == 1) {
		AfxMessageBox(L"该状态已完成不能设定？");
		return;
	}
	if (AfxMessageBox(L"你确定执行下一个角色么？", MB_YESNO) == IDYES)
	{

		//int nState = g_AccountFile.GetInt(NumberToString(nIndex + 1), L"state");
		//if (nState == 1) {
		//	AfxMessageBox(L"不能设置已完成数据");
		//	return;
		//}
		//如果选中扎昆
		if (m_ZhaKun.GetCheck() == 1) {
			
			int nRoleNum = g_AccountFile.GetInt(NumberToString(nIndex + 1), L"RoleNum")+1;
			int nZhaNum = g_AccountFile.GetInt(NumberToString(nIndex + 1), L"ZhaNum");
			if (nRoleNum == nZhaNum+1) {
				AfxMessageBox(L"已经为最后一个角色");
				return;
			}
			g_main_cwnd->m_list_info.SetItemText(nIndex, 7,NumberToString( nRoleNum));
			g_AccountFile.SetInt(NumberToString(nIndex + 1), L"RoleNum", nRoleNum);
			//g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"1");
			g_nState = 10;
		}

	}
}


BOOL CGameDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		int nVirtKey = (int)pMsg->wParam;
		if (nVirtKey == VK_RETURN || nVirtKey == VK_ESCAPE)
		{
			return TRUE;
		}
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}


void CGameDlg::OnBnClickedBtnBack()
{
	// TODO: 在此添加控件通知处理程序代码
	if (AfxMessageBox(L"是否确定备份数据？", MB_YESNO) == IDYES)
	{
		if (PathFileExists(L"AccountBack.ini")) {
			DeleteFile(L"AccountBack.ini");
			//g_dm->DeleteFileW(L"AccountBack.ini");
		}

		BOOL m = CopyFile(L"Account.ini", L"AccountBack.ini", TRUE);
	}

//	g_dm->CopyFileW(L"Account.ini",L"AccountBack.ini",1);
//
//	g_dm->MoveFileW(L"Account.ini", L"AccountBack.ini");
}


void CGameDlg::OnBnClickedBtnRest()
{
	// TODO: 在此添加控件通知处理程序代码
	if (AfxMessageBox(L"是否确定还原数据？", MB_YESNO) == IDYES)
	{
		if (PathFileExists(L"Account.ini")) {
			DeleteFile(L"Account.ini");
			//g_dm->DeleteFileW(L"AccountBack.ini");
		}
		BOOL m = CopyFile(L"AccountBack.ini", L"Account.ini", TRUE);
		InitConfig();
	}
}
